Ext.ns("sys");

sys.QueryUnitForm = Ext.extend(Ext.form.FormPanel, {
	constructor : function(cfg) {
		Ext.apply(this, cfg || {});

		this.unitId = new Ext.form.Hidden({
			fieldLabel : 'id',
			name : 'id'
		});
		this.key = new Ext.form.TextField({
			fieldLabel : '编号',
			name : 'key',
			hiddenName : 'key',
			allowBlank : false
		});
		this.type = new Ext.form.ComboBox({
			mode : 'local',
			fieldLabel : '类型',
			hiddenField : 'key',
			valueField : 'key',
			displayField : 'value',
			emptyText : '请选择......',
			editable : false,
			name : 'type',
			hiddenName : 'type',
			editable : false,
			triggerAction : 'all',
			allowBlank : false,
			store : new Ext.data.SimpleStore({
				data : [ [ 'hql', 'hql' ], [ 'sql', 'sql' ] ],
				fields : [ 'key', 'value' ]
			})
		});
		this.text = new Ext.form.TextArea({
			fieldLabel : '内容',
			name : 'text'
		});
		this.description = new Ext.form.TextField({
			fieldLabel : '描述',
			name : 'description'
		});
		this.saveBtn = new Ext.Button({
			text : '新建',
			formBind : true,
			handler : function() {
				this.form.doAction('submit', {
					url : '../../query/saveQueryUnit.do',
					method : 'POST',
					scope : this,
					success : function(form, action) {
						this.reset();
						this.queryUnitPanel.queryUnitGrid.refresh();
					},
					failure : function(form, action) {
						switch (action.failureType) {
						case Ext.form.Action.CLIENT_INVALID:
							Ext.Msg.alert("提示", "提交数据无效");
							break;
						case Ext.form.Action.CONNECT_FAILURE:
							Ext.Msg.alert("提示", "与服务器连接失败");
							break;
						case Ext.form.Action.SERVER_INVALID:
							Ext.Msg.alert("提示", action.result.msg);
						}
					}
				});
			},
			scope : this
		});
		this.reset = function() {
			this.form.reset();
			this.saveBtn.setText('新建');
		};

		this.edit = function(rec) {
			this.form.loadRecord(rec);
			this.saveBtn.setText('保存');
		};

		sys.QueryUnitForm.superclass.constructor.call(this,
				{
					id : '_query_unit_form',
					baseCls : 'x-plain',
					labelAlign : 'right',
					labelWidth : 60,
					bodyStyle : "padding: 5px 5px 5px 5px;",
					monitorValid : true,
					defaults : {
						width : 300
					},
					items : [ this.unitId, this.key, this.type, this.text,
							this.description ],
					buttons : [ this.saveBtn ]
				});
	}
});

sys.QueryUnitGrid = Ext.extend(Ext.grid.GridPanel, {
	constructor : function(cfg) {
		Ext.apply(this, cfg || {});

		this.delBtn = new Ext.Button({
			text : '删除',
			iconCls : 'del',
			disabled : true,
			verify : function(sm) {
				return sm.getSelections().length > 0;
			},
			handler : function() {
				var ids = this.getSelectedIds();
				Ext.Msg.confirm('提示', '是否删除选中项,共[' + ids.length + ']条', function(msg) {
					if (msg == 'yes') {
						Ext.Ajax.request({
							url : '../../query/delQueryUnits.do',
							method : 'POST',
							success : function(resp, opt) {
								this.refresh();
							},
							params : {
								ids : ids
							},
							scope : this
						});
					}
				}, this);
			},
			scope : this
		});
		this.searcher = new Ext.form.TwinTriggerField({
			width : 150,
			trigger1Class : 'x-form-clear-trigger',
			trigger2Class : 'x-form-search-trigger',
			hideTrigger1 : true,
			emptyText : '输入关键字查询',
			grid : this,
			onTrigger1Click : function(e) {
				this.reset();
				this.onTrigger2Click(e);
			},
			onTrigger2Click : function(e) {
				var text = this.getValue().trim();
				if (text) {
					Ext.apply(this.grid.getStore().baseParams, {
						queryText : text
					});
					this.triggers[0].show();
				} else {
					this.reset();
					this.triggers[0].hide();
					Ext.destroyMembers(this.grid.getStore().baseParams, 'queryText');
				}
				this.grid.refresh();
			},
			listeners : {
				'specialkey' : {
					fn : function(fild, e) {
						if (e.getKey() == e.ENTER) {
							this.searcher.onTrigger2Click.call(this.searcher);
						}
					},
					scope : this
				}
			}
		});
		this.store = new Ext.data.JsonStore({
			url : '../../query/pageQuery.do',
			method : 'POST',
			baseParams : {
				type : 'hql',
				text : 'from QueryUnit qu where 1=1',
				'querySegments[0].condition' : 'queryText!=void',
				'querySegments[0].text' : "and (qu.key like '%${queryText}%' or qu.text like '%${queryText}%')"
			},
			totalProperty : 'count',
			root : 'data',
			fields : [ 'id', 'key', 'type', 'text', 'querySegments', 'description' ]
		});
		this.csm = new Ext.grid.CheckboxSelectionModel({
			listeners : {
				'selectionchange' : {
					fn : function(sm) {
						var queryConfigPanel = this.queryUnitPanel.queryConfigPanel;
						var querySegmentPanel = queryConfigPanel.querySegmentPanel;
						var querySegmentGrid = querySegmentPanel.querySegmentGrid;
						var querySegmentForm = querySegmentPanel.querySegmentForm;
						var segmentStore = querySegmentGrid.getStore();

						this.queryUnitPanel.queryUnitForm.reset();
						var rec = sm.getSelected();
						if (rec) {
							this.queryUnitPanel.queryUnitForm.edit(rec);
							querySegmentForm.form.setValues({
								'queryUnit.id' : rec.data['id']
							});
						} else {
							querySegmentForm.queryUnitId.reset();
						}
						querySegmentGrid.refresh();
						Ext.each(this.getTopToolbar().items.getRange(), function(item,
								index, all) {
							if (item.verify) {
								item.setDisabled(!item.verify(sm));
							}
						}, this);
					},
					scope : this
				}
			}
		});

		this.cm = new Ext.grid.ColumnModel({
			defaults : {
				sortable : true,
				width : 100
			},
			columns : [ new Ext.grid.RowNumberer(), this.csm, {
				id : 'key',
				header : '编号',
				dataIndex : 'key',
				width : 150
			}, {
				id : 'type',
				header : '类型',
				dataIndex : 'type'
			}, {
				id : 'text',
				header : '内容',
				dataIndex : 'text'
			}, {
				id : 'description',
				header : '描述',
				dataIndex : 'description'
			} ]
		});
		this.refresh = function() {
			this.getSelectionModel().clearSelections();
			this.getBottomToolbar().doLoad(this.getBottomToolbar().cursor);
		};
		this.getSelected = function() {
			return this.getSelectionModel().getSelected();
		};
		this.getSelections = function() {
			return this.getSelectionModel().getSelections();
		};
		this.getSelectedIds = function() {
			var ids = [];
			var records = this.getSelectionModel().getSelections();
			if (!records)
				return ids;
			for ( var i = 0; i < records.length; i++) {
				ids.push(records[i].data.id);
			}
			return ids;
		};

		sys.QueryUnitGrid.superclass.constructor.call(this, {
			id : '_query_unit_grid',
			store : this.store,
			loadMask : true,
			stripeRows : true,
			sm : this.csm,
			cm : this.cm,
			autoExpandColumn : 'text',
			tbar : [ this.delBtn, '-', this.searcher ],
			bbar : new Ext.PagingToolbar({
				store : this.store,
				displayInfo : true
			}),
			listeners : {
				'render' : {
					fn : function() {
						this.refresh();
					},
					scope : this
				}
			}
		});
	}

});

sys.QueryUnitPanel = Ext.extend(Ext.Panel, {
	constructor : function(cfg) {
		Ext.apply(this, cfg || {});
		this.queryUnitForm = new sys.QueryUnitForm({
			region : 'center',
			queryUnitPanel : this
		});
		this.queryUnitGrid = new sys.QueryUnitGrid({
			title : '查询配置列表',
			region : 'center',
			queryUnitPanel : this
		});
		sys.QueryUnitPanel.superclass.constructor.call(this, {
			id : '_query_unit_panel',
			layout : 'border',
			title : '查询单元信息',
			items : [ {
				title : '编辑查看',
				region : 'north',
				height : 240,
				layout : 'border',
				split : true,
				collapsible : true,
				items : [ this.queryUnitForm ]
			}, this.queryUnitGrid ]
		});
	}
});